<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 满足三条件之一需改变的最少字符数 -->
    <script>
      var minCharacters = function (a, b) {
        let da = new Array(26).fill(0)
        let db = new Array(26).fill(0)
        for (let i in a) {
          da[a.charCodeAt(i) - 97]++
        }
        for (let i in b) {
          db[b.charCodeAt(i) - 97]++
        }
        let an = a.length
        let bn = b.length
        let asum = 0
        let bsum = 0
        let res = Number.MAX_SAFE_INTEGER
        for (let i = 0; i < 25; i++) {
          // 前缀和计算
          asum += da[i]
          bsum += db[i]
          // 找最小值
          res = Math.min(res, an + bn - da[i] - db[i], an - asum + bsum, bn - bsum + asum)
        }
        // z的特殊处理
        return Math.min(res, an + bn - da[25] - db[25])
      }
      console.log(minCharacters('aba', 'caa'))
    </script>
  </body>
</html>
